package activemq_chap7;

import java.util.ArrayList;
import java.util.List;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerPlugin;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.security.AuthenticationUser;
import org.apache.activemq.security.SimpleAuthenticationPlugin;

/**
 * Khoi tao va chay 1 broker thay the cho lenh console
 * 
 * @author root
 * 
 */
public class DemoBrokerServices {

	public static void main(String[] args) {
		BrokerService brokerService = new BrokerService();
		brokerService.setBrokerName("myBroker");
		brokerService.setDataDirectory("data/");
		// create simple authentication plugin
		SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin();
		// create list authentication user
		List<AuthenticationUser> authenticationUsers = new ArrayList<AuthenticationUser>();
		// add user to list
		authenticationUsers.add(new AuthenticationUser("admin", "admin",
				"admins,publishers,consumers"));
		authenticationUsers.add(new AuthenticationUser("consumers",
				"consumers", "consumers"));
		authenticationUsers.add(new AuthenticationUser("publishers",
				"publishers", "publishers"));
		authenticationUsers.add(new AuthenticationUser("guest", "guest",
				"guest"));
		// set list user for simple authentication
		authenticationPlugin.setUsers(authenticationUsers);
		// set plugin fo Broker Services
		brokerService.setPlugins(new BrokerPlugin[] { authenticationPlugin });
		// add connector
		try {
			brokerService.addConnector(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
			brokerService.start();
			System.out.println("Start BrokerServices Success !");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
